package cn.edu.pzhu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.pzhu.pojo.Msg;
import cn.edu.pzhu.pojo.User;
import cn.edu.pzhu.pojo.UserInfo;
import cn.edu.pzhu.service.UserInfoService;
import cn.edu.pzhu.service.UserService;
import cn.edu.pzhu.service.imp.UserInfoServiceImp;
import cn.edu.pzhu.service.imp.UserServiceImp;

/**
 * Servlet implementation class CancelServlet
 */
@WebServlet("/cancel")
public class CancelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CancelServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		Object User = session.getAttribute("user");
		String username = request.getParameter("username");
		
		if (User instanceof User user) {
			UserService us = new UserServiceImp();
			Msg msg = us.destory(user.getUsername());
			UserInfoService uis = new UserInfoServiceImp();
			Msg umsg = uis.deleteUserInfo(username);
			
			if (msg.isSuccess() && umsg.isSuccess()) {
				//注销当前会话
				request.getSession().invalidate();
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().print("<script>alert('账号注销成功！');location.href='regist.jsp'</script>");
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
